<template>
	<view class="">
		<view class="exchange_bg">
			<view class="jui_bg_fff jui_lrtop box jui_flex jui_flex_justify_between ">
				
				<view class="container">
				  <form class="luckform" @submit="formSubmit">
				
				    <view class="items">
				      <view class="item weui-cell">
				        <view class="weui-cell__hd">
				          <view class="weui-label">项目名称<span class="span_required">*</span></view>
				        </view>
				        <view class="weui-cell__bd">
				          <input name="phone" class="weui-input" type="text" placeholder="请输入项目名称" v-model="task.name" />
				        </view>
				      </view>
				      <view class="item weui-cell">
				        <view class="weui-cell__hd">
				          <view class="weui-label">申请总资金<span class="span_required">*</span></view>
				        </view>
				        <view class="weui-cell__bd">
				          <input name="price" class="weui-input" type="number" placeholder="¥" v-model="task.price" />
				        </view>
				      </view>
				    </view>
				
				    <view class="items custom_checkbox">
				      <view class="top" style="display: flex;">
				        <span class="title">设置里程碑</span><span class="span_required">*</span>
						<button style="width: 60px; height: 30px; line-height: 30px; margin-top: -5px; margin-left: 12px; font-size: 14px;" @tap="addItem">添加</button>
				      </view>
				      <view class="" v-for="(item, index) in task.inputItems" :key="index">
				         <view class="item weui-cell" >
				              <view class="weui-cell__hd">
				                <view class="weui-label">里程碑{{index+1}}目标<span class="span_required">*</span></view>
				              </view>
				              <view class="weui-cell__bd">
				                <input name="text" class="weui-input" type="text" placeholder="简述该里程目标" v-model="item.text" />
				              </view>
				         </view>
						 <view class="item weui-cell" >
						      <view class="weui-cell__hd">
						        <view class="weui-label">里程碑{{index+1}}资金<span class="span_required">*</span></view>
						      </view>
						      <view class="weui-cell__bd">
						        <input name="price" class="weui-input" type="number" placeholder="¥" v-model="item.price" />
						      </view>
						 							  
						      
						      
						 </view>
						 <!-- <view class="item weui-cell" >
						      <view class="weui-cell__hd">
						        <view class="weui-label">里程碑{{index+1}}时限<span class="span_required">*</span></view>
						      </view>
						      <view class="weui-cell__bd">
						        <input name="day" class="weui-input" type="number" placeholder="天" v-model="item.day" />
						      </view>
						 							  
						      <button style="width: 60px; height: 30px; line-height: 30px; font-size: 14px;" @tap="delItem(index)">移除</button>
						      
						 </view> -->
				      </view>
				      
				    </view>
				
				    <view class="items">
				      <view class="top">
				        <span class="title">项目描述</span><span class="span_required">*</span>
				      </view>
				      <view class="item weui-cell">
				        <view class="weui-cell__bd">
				          <textarea v-model="task.task_desc" maxlength="-1" name="content" class="weui-textarea" placeholder="请输入详细介绍,以便于投资及审核"></textarea>
				        </view>
				      </view>
				    </view>
					<view class="item weui-cell">
					  <view class="weui-cell__hd">
					    <view class="weui-label">奖励类型<span class="span_required">*</span></view>
					  </view>
					  <view class="weui-cell__bd">
					    <radio-group name="r" class="weui-input" @change="radioChange">
							<label class="radio" style="margin-right: 50rpx;"><radio style="transform:scale(0.7)" value="股权" checked="true" />股权</label>
							<label class="radio"><radio style="transform:scale(0.7)" value="奖励" />奖励</label>
						</radio-group> 
					  </view>
					</view>
					<view class="items">
					  <view class="top">
					    <span class="title">奖励明细说明</span><span class="span_required">*</span>
					  </view>
					  <view class="item weui-cell">
					    <view class="weui-cell__bd">
					      <textarea v-model="task.reward_desc" maxlength="-1" name="content" class="weui-textarea" placeholder="请输入详细奖励明细说明,以便于投资及审核,奖励明细请对应每个里程碑细节,否则不予通过"></textarea>
					    </view>
					  </view>
					</view>
				    <view class="btnbox">
				      <!-- <button class="luckbtn luckbtn-default" formType="submit">提交信息</button> -->
					  <view class="zhuse  shouquan" @tap='chongzhi'>
					  	提交审核
					  </view>
				    </view>
				  </form>
				
				</view>
				
				
			</view>
		</view>
	</view>
</template>

<script>
	import vNavigation from '@/components/navigation/navigation.vue'
	export default {
		components: {
			vNavigation
		},
		data() {
			return {
				task: {
					name: '',
					price:'',
					inputItems: [{ // 初始数据，至少包含一项
					        price: '',
							text: '',
					      }],
					task_desc: '',
					reward_type: '股权',
					reward_desc: '',
					
					
				},
			}
		},
		onLoad() {
			
		},
		onShow() {
			// this.exchange()
			// this.getFromToken()
			if(!uni.getStorageSync('loginInfo').user_id){
				uni.navigateTo({
					url:'/pages/login/login'
				})
			}
		},
		methods: {
			formatDate(date) {
			    let year = date.getFullYear();
			    let month = ("0" + (date.getMonth() + 1)).slice(-2); // 月份从0开始，需要加1，并补零到两位数
			    let day = ("0" + date.getDate()).slice(-2); // 补零到两位数
			
			    return `${year}-${month}-${day}`;
			},
			chongzhi(){
				if (this.task.name == '') {
					this.$msg('项目名称');
					return false;
				}
				if (this.task.price == '') {
					this.$msg('请输入项目金额');
					return false;
				}
				if (this.task.inputItems.length == 0) {
					this.$msg('请设置目标');
					return false;
				}
				if (!this.task.task_desc) {
					this.$msg('请填写项目描述');
					return false;
				}
				if (!this.task.reward_desc) {
					this.$msg('请填写奖励描述');
					return false;
				}
				var tmp = 0
				var dd = 0
				for (var i=0;i<this.task.inputItems.length;i++)
				{ 
				    tmp = tmp + parseInt(this.task.inputItems[i].price);
					// if(this.task.inputItems[i].day == undefined|| this.task.inputItems[i].day <=0){
					// 	this.$msg('时限不能小于0天');
					// 	return false;
					// }else if(this.task.inputItems[i].day >180){
					// 	this.$msg('时限不能大于180天');
					// 	return false;
					// }else{
					// 	let currentDate = new Date();
					// 	dd = dd + this.task.inputItems[i].day
					// 	currentDate.setDate(currentDate.getDate() + dd);
					// 	let futureDateStr = this.formatDate(currentDate);
					// 	console.log(futureDateStr);
					// 	this.task.inputItems[i].last_day = futureDateStr
					// }
				}
				if (tmp != this.task.price) {
					this.$msg('里程碑资金分配错误');
					return false;
				}
				var that= this;
				uni.showModal({
					content:'请确认您的材料及内容是否填写完整！！',
					success() {
						var data = that.task
						data.user_id = uni.getStorageSync('loginInfo').user_id
						that.$api.add_task({task: data }).then(res => {
							
							if(res.code == 10000){
								that.$msg('创建成功，请等待初审');
								setTimeout(() => {
									uni.reLaunch({
										url: '/pages/index/index'
									})
								}, 2000)
							}
							else{
								that.$msg('您还未认证，不能申报项目');
							}
						});
					}
				})
				console.log(this.task)
			},
			radioChange: function(evt) {
			           console.log(evt.detail.value)
					   this.task.reward_type = evt.detail.value
			        },
			addItem() {
			      this.task.inputItems.push({ // 每次点击按钮就在数组末尾添加一个新的输入项
			        price: '',
					text: ''
			      });
			    },
			delItem(index) {
				console.log(index)
			      this.task.inputItems.splice(index,1);
			    },
			
		}
	}
</script>

<style lang="scss">
	@import url("@/static/css/weui.min.css");
	page {
		background-color: #FFFFFF;
	}
	.zhusecol {
		color: $zhuse;
	}
	.exchange_bg {
		background: $zhuse;
		// padding-top: 90rpx;
		.box {
			padding: 70rpx 30rpx 0 30rpx;
			flex-wrap: wrap;
		}
	}

	.topPlace {
		width: 100%;
		height: var(--status-bar-height);
		background: $zhuse;

	}

	.hb {
		width: 322rpx;
		height: 322rpx;
		background: #FFFFFF;
		box-shadow: 0px 0px 10rpx 0rpx rgba(0, 0, 0, 0.1);
		border-radius: 32rpx;
		text-align: center;
		margin-bottom: 30rpx;
	}
	
	
	.luckform .items {
	  margin-top: 30rpx;
	}
	.luckform .btnbox {
	  margin-top: 50rpx;
	}
	.shouquan {
		margin-top: 50px;
		margin-bottom: 20px;
		width: 40%;
		margin-left: 30%;
		height: 82rpx;
		background: $zhuse;
		border-radius: 20rpx;
		line-height: 82rpx;
		text-align: center;
		color: #FFFFFF;
	}
</style>
